#!/usr/bin/perl
# $Id$
#
# Description:
# Automatically translates .po files based on extracted terms from previous
# translations


use strict;

use constant DEFINITIONS => "../Makefile.in";

use File::Find qw(find);
use File::Path qw(rmtree);


my %term;
my ($AUTOMATED_TERMS, $POOTLE_DIR);

my ($project, $old_project_dir, $new_project_dir);
my ($po_file, $po_path, $po_dir);

print "Reading definitions file ".&DEFINITIONS."\n";
open (INPUT_FILE, &DEFINITIONS) || die $!;
while (<INPUT_FILE>) {

	chomp;

	/(.+) := (.+)/;

	my $key = $1;
	my $value = $2;

	if ($key =~ /automated_terms_file/i) {
		$AUTOMATED_TERMS = $value;
	} elsif ($key =~ /pootle_dir/i) {
		$POOTLE_DIR = $value;
	}

}
print "\n";

# Establish excpetion list for capitalization
my %exception;
for (qw( te he ngā a e i o u mō mā me ki ka to nā nō)) {
	$exception{$_}++;
}


print "Populating terms\n";
open( TERMS, $AUTOMATED_TERMS ) || die $!;
while ( <TERMS> ) {
	chomp;
	/(.*)\t(.*)/;
	$term{$1} = $2;
}
close(TERMS);
print "\n";


# Read in command line argument
$project = $ARGV[0];
$old_project_dir = $POOTLE_DIR."/".$project;
die "Please specify a project to auto-translate." if (not defined $project);
die 'Project "'.$old_project_dir.'"does not exist.' if (! -d $old_project_dir);
$new_project_dir = $old_project_dir.".automated";


print "(Re)creating directories\n";
rmtree( $new_project_dir );
system( 'cd "'.$old_project_dir.'"; find . -type d | grep -v \'\.svn\' | sed -e \'s|^\.|\.\.\/'.$new_project_dir.'|\' | xargs mkdir -p' );
print "\n";

sub tc {
# Taken from the Perl Cookbook - Section 1.14
	local $_ = shift;
	s/([\wāēīōū]+)/$exception{$1} ? lc($1) : ucfirst(lc($1))/gie;
	return $_;
}


sub process_file() {

	my $old_file = $_;
	my $old_path = $File::Find::name;
	my $old_dir = $File::Find::dir;

	return unless ($old_file =~ /\.po$/);

	my $new_path = $old_path;
	$new_path =~ s/^$old_project_dir/$new_project_dir/;

	print "Processing ".$old_path." -> ".$new_path."\n";

	open (INPUT, $old_path) || die;
	open (OUTPUT, '>', $new_path) || die;

	my $translation;
	while ( <INPUT> ) {
		my $line = $_;
		if ( $line =~ /msgid "(.+)"/ ) {
			my %rep;
			my $msgid = $1;

			# Normalize msgid for term lookup
			my $lookup = $msgid;
			$lookup =~ s/[&]//g; # Remove accessibility keys
			$rep{'cont'} = 1 if ( $lookup =~ s/…$// ); # Remove continuation mark
			$rep{'end_colon'} = 1 if ( $lookup =~ s/:$// ); # Remove end colon
			$rep{'full_stop'} = 1 if ( $lookup =~ s/\.$// ); # Remove full-stop
			$rep{'bracket'} = 1 if ( $lookup =~ s/^\((.*)\)$/\1/ ); # Remove ()'s
			$rep{'ang_bracket'} = 1 if ( $lookup =~ s/^<(.*)>$/\1/ ); # Remove <>'s

			chomp( $lookup );
			if ( $lookup =~ /[[:upper:]]/ ) {
				$rep{'caps'} = 1;
			} else {
				$rep{'caps'} = 0;
			}
			if ( $lookup =~ /^[[:upper:]]/ ) {
				$rep{'cap1'} = 1;
			} else {
				$rep{'cap1'} = 0;
			}
			$lookup = lc( $lookup );

			if ( $translation = $term{$lookup} ) {
				$translation = $translation . "…" if ( defined $rep{'cont'} );
				$translation = $translation . ":" if  ( defined $rep{'end_colon'} );
				$translation = $translation . "." if ( defined $rep{'full_stop'} );
				$translation = "(" . $translation . ")" if ( defined $rep{'bracket'} );
				$translation = "<" . $translation . ">" if ( defined $rep{'ang_bracket'} );
				$translation = tc($translation) if ( $rep{'caps'} == 1 ); # Capitalize key words only
				$translation = ucfirst($translation) if ( $rep{'cap1'} == 1 ); # Capitalize first word
				undef %rep;
			}

		}

		if (defined $translation && $line =~ /^msgstr/) {

			# if no prior translation exists
			if ($line =~ /^msgstr ""/) {
				$line = 'msgstr "' . $translation . '"' . "\n";
			}

			print $line;
			undef $translation;
		}
		print OUTPUT $line;
	}

	close INPUT;
	close OUTPUT;

	print "\n";

}


# Read through each .PO file and output to PO file with the translated term
find( { wanted => \&process_file, no_chdir => 1 }, $old_project_dir );
